import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login'

Vue.use(Router)

function load(component) {
    // '@' is aliased to src
    return () => import(`@/components/${component}.vue`)
}

let routes = [
    {
        path: '/',
        name: 'Home',
        component: load('Home'),
        children: [
            {
                path: '/search',
                component: load('views/SearchResult'),
                name: 'SearchResult',
                meta: {requiresAuth: true, desc: 'Search Result'}
            },
            {
                path: '/dashboard',
                component: load('views/Dashboard'),
                name: 'dashboard',
                meta: {requiresAuth: true, desc: 'Dashboard'}
            },
            {
                path: '/physical',
                component: load('views/PhysicalRoom'),
                name: 'PhysicalRoom',
                meta: {requiresAuth: true, desc: 'Physical Room List'}
            },
            /*2020.02.13 Mr.zhang Add Start*/
            {
              path: 'PhysicalRoomListing-V2',
              component: load('views/PhysicalRoomListing-V2'),
              name: 'PhysicalRoomListing-V2',
              meta: {requiresAuth: true, desc: 'Physical Room Listing V2'}
            },
            {
              path: '/PhysicalRoomListingEdit',
              component: load('views/PhysicalRoomListingEdit'),
              name: 'PhysicalRoomListingEdit',
              meta: {requiresAuth: true, desc: 'Physical Room Listing Edit'}
            },/*2020.02.13 Mr.zhang Add End*/

            {
              path: '/FunkeyWorkerTaskList',
              component: load('views/FunkeyWorkerTaskList'),
              name: 'FunkeyWorkerTaskList',
              meta: {requiresAuth: true, desc: 'Funkey WorkerTask List'}
            },/*2020.02.13 Mr.zhang Add End*/
            {
              path: '/PhysicalRoomReviews',
              component: load('views/PhysicalRoomReviews'),
              name: 'PhysicalRoomReviews',
              meta: {requiresAuth: true, desc: 'Funkey PhysicalRoomReviews List'}
            },/*2020.03.05 Mr.wang Add End*/
            {
                path: '/PhysicalRoomGallery',
                component: load('views/PhysicalRoomGallery'),
                name: 'PhysicalRoomGallery',
                meta: {requiresAuth: true, desc: 'Physical Room Gallery'}
            },
            {
                path: '/PhysicalRoomMedia',
                component: load('views/PhysicalRoomMedia'),
                name: 'PhysicalRoomMedia',
                meta: {requiresAuth: true, desc: 'Physical Room Media'}
            },
            {
                path: '/PhysicalRoomServiceInfo',
                component: load('views/PhysicalRoomServiceInfo'),
                name: 'PhysicalRoomServiceInfo',
                meta: {requiresAuth: true, desc: 'Physical Room ServiceInfo'}
            },
          /*2019.12.28 Mr.li Add Start*/
          {
            path: '/HourseStatusOnCalendar',
            component: load('views/HourseStatusOnCalendar'),
            name: 'HourseStatusOnCalendar',
            meta: {requiresAuth: true, desc: 'HourseStatus On Calendar'}
          },
          {
            path: '/PriceSettingOnCalendar',
            component: load('views/PriceSettingOnCalendar'),
            name: 'PriceSettingOnCalendar',
            meta: {requiresAuth: true, desc: 'Price Setting On Calendar'}
          }, /*2019.12.28 Mr.li Add End*/
          {/*2019.01.13 Mr.zhang Add End*/
            path: '/NewHourseSourceAdd',
            component: load('views/NewHourseSourceAdd'),
            name: 'NewHourseSourceAdd',
            meta: {requiresAuth: true, desc: 'New Hourse Source Add'}
          },/*2019.01.13 Mr.zhang Add End*/
          // {
          //   path: '/PriceSettingOnCalendar1',
          //   component: load('views/PriceSettingOnCalendar1'),
          //   name: 'PriceSettingOnCalendar1',
          //   meta: {requiresAuth: true, desc: 'Price Setting On Calendar1'}
          // },

            {
                path: '/PhysicalRoomListing',
                component: load('views/PhysicalRoomListing'),
                name: 'PhysicalRoomListing',
                meta: {requiresAuth: true, desc: 'Physical Room Listing'}
            },
            {
                path: '/PhysicalRoomListingDescription',
                component: load('views/PhysicalRoomListingDescription'),
                name: 'PhysicalRoomListingDescription',
                meta: {requiresAuth: true, desc: 'Physical Room Listing Description'}
            },
            {
                path: '/PhysicalRoomAmenityCategory',
                component: load('views/PhysicalRoomAmenityCategory'),
                name: 'PhysicalRoomAmenityCategory',
                meta: {requiresAuth: true, desc: 'Physical Room Amenity Category'}
            },
            {
                path: '/PhysicalRoomGuestControls',
                component: load('views/PhysicalRoomGuestControls'),
                name: 'PhysicalRoomGuestControls',
                meta: {requiresAuth: true, desc: 'Physical Room Guest Controls'}
            },
            {
                path: '/PhysicalRoomGuestExpectation',
                component: load('views/PhysicalRoomGuestExpectation'),
                name: 'PhysicalRoomListingExpectationsForGuests',
                meta: {requiresAuth: true, desc: 'Physical Room Guest Expectations'}
            },
            {
                path: '/PhysicalRoomBookingSettings',
                component: load('views/PhysicalRoomBookingSettings'),
                name: 'PhysicalRoomBookingSettings',
                meta: {requiresAuth: true, desc: 'Physical Room Booking Settings'}
            },
            {
                path: '/PhysicalRoomAvailabilityRule',
                component: load('views/PhysicalRoomAvailabilityRule'),
                name: 'PhysicalRoomAvailabilityRule',
                meta: {requiresAuth: true, desc: 'Physical Room Availability Rule'}
            },
            {
                path: '/PhysicalRoomSeasonalMinNights',
                component: load('views/PhysicalRoomSeasonalMinNights'),
                name: 'PhysicalRoomSeasonalMinNights',
                meta: {requiresAuth: true, desc: 'Physical Room Seasonal Min Nights'}
            },
            {
                path: '/PhysicalRoomPricingSetting',
                component: load('views/PhysicalRoomPricingSetting'),
                name: 'PhysicalRoomPricingSetting',
                meta: {requiresAuth: true, desc: 'Physical Room Price Setting'}
            },
            {
                path: '/AirbnbUser',
                component: load('views/AirbnbUser'),
                name: 'AirbnbUser',
                meta: {requiresAuth: true, desc: 'Airbnb User'}
            },
            {
                path: '/physicalroomcalendar/:pid',
                component: load('views/PhysicalRoomCalendar'),
                name: 'PhysicalRoomCalendar',
                meta: {requiresAuth: true, desc: 'Physical Room Calendar'}
            },
            {
                path: '/logicalRoomcalendar/:lid',
                component: load('views/LogicalRoomCalendar'),
                name: 'LogicalRoomCalendar',
                meta: {requiresAuth: true, desc: 'Logical Room Calendar'}
            },
            {
                path: '/logical',
                component: load('views/LogicalRoom'),
                name: 'LogicalRoom',
                meta: {requiresAuth: true, desc: 'Logical Room List'}

            },
            {
                path: '/events',
                component: load('views/Events'),
                name: 'Events',
                meta: {requiresAuth: true, desc: 'Events List'}

            },
            {
                path: '/GuestRegistration',
                component: load('views/GuestRegistration'),
                name: 'GuestRegistration',
                meta: {requiresAuth: true, desc: 'Guest Registration List'}
            },
            {
                path: '/GuestRegistrationThailand',
                component: load('views/GuestRegistrationThailand'),
                name: 'ThailandGuestRegistration',
                meta: {requiresAuth: true, desc: 'Thailand Guest Registration List'}
            },
            {
                path: '/order',
                component: load('views/Order'),
                name: 'Order',
                meta: {requiresAuth: true, desc: 'Order List'}
            },
            {
                path: '/insertorder',
                component: load('views/InsertOrder'),
                name: 'InsertOrder',
                meta: {requiresAuth: true, desc: 'Insert Order And Ical'}
            },
            {
                path: '/host',
                component: load('views/Host'),
                name: 'Host',
                meta: {requiresAuth: true, desc: 'Host List'}
            },
            {
                path: '/company',
                component: load('views/Company'),
                name: 'Company',
                meta: {requiresAuth: true, desc: 'Company List'}
            },
            // Okumasan
            // {
            //     path: '/okumasanhost',
            //     component: load('views/Okumasan/OkumasanHost'),
            //     name: 'OkumasanHost',
            //     meta: {requiresAuth: true, desc: 'Okumasan Host List'}
            // },
            {
                path: '/okumasancleaner',
                component: load('views/Okumasan/OkumasanCleaner'),
                name: 'OkumasanCleaner',
                meta: {requiresAuth: true, desc: 'Okumasan cleaner List'}
            },
            {
                path: '/okumasanroom',
                component: load('views/Okumasan/OkumasanRoom'),
                name: 'OkmRoom',
                meta: {requiresAuth: true, desc: 'Okumasan Room List'}
            },
            {
                path: '/okumasanorder',
                component: load('views/Okumasan/OkumasanOrder'),
                name: 'OkmOrder',
                meta: {requiresAuth: true, desc: 'Okumasan Order List'}
            },
            {
                path: '/deliveryorder',
                component: load('views/Okumasan/DeliveryOrder'),
                name: 'DeliveryOrder',
                meta: {requiresAuth: true, desc: 'Delivery Order List'}
            },
            {
                path: '/otherexpenses',
                component: load('views/Okumasan/OtherExpenses'),
                name: 'OtherExpenses',
                meta: {requiresAuth: true, desc: 'Other Expense List'}
            },
            {
                path: '/recurringexpense',
                component: load('views/Okumasan/RecurringExpense'),
                name: 'RecurringExpense',
                meta: {requiresAuth: true, desc: 'Recurring Expense List'}
            },
            {
                path: '/ical2clean',
                component: load('views/Okumasan/Ical2Clean'),
                name: 'Ical2Clean',
                meta: {requiresAuth: true, desc: 'Clean List From Ical'}
            },
            // Notification
            {
                path: '/CustomInformation',
                component: load('views/Notification/CustomInformation'),
                name: 'CustomInformation',
                meta: {requiresAuth: true, desc: 'Custom Information'}
            },
            {
                path: '/duplicateorder',
                component: load('views/Notification/DuplicateOrder'),
                name: 'DuplicateOrder',
                meta: {requiresAuth: true, desc: 'Duplicate Order'}
            },
            {
                path: '/duplicateical',
                component: load('views/Notification/DuplicateIcal'),
                name: 'DuplicateIcal',
                meta: {requiresAuth: true, desc: 'Duplicate Ical'}
            },
            {
                path: '/checkstatus',
                component: load('views/Notification/CheckStatus'),
                name: 'CheckStatus',
                meta: {requiresAuth: true, desc: 'Check Status of bnb_alt and cancel_pre'}
            },
            {
                path: '/checkbill',
                component: load('views/Notification/CheckBill'),
                name: 'CheckBill',
                meta: {requiresAuth: true, desc: 'Check some questionable order'}
            },
            {
                path: '/ical2order',
                component: load('views/Notification/Ical2Order'),
                name: 'Ical2Order',
                meta: {requiresAuth: true, desc: 'Orders without ical'}
            },
            {
                path: '/order2ical',
                component: load('views/Notification/Order2Ical'),
                name: 'Order2Ical',
                meta: {requiresAuth: true, desc: 'Icals without order'}
            },
            {
                path: '/issues/:type',
                component: load('views/Notification/Issues'),
                name: 'Issues',
                meta: {requiresAuth: true, desc: 'Issues Check'}
            },
            {
                path: '/checklist',
                component: load('views/Notification/CheckList'),
                name: 'CheckList',
                meta: {requiresAuth: true, desc: 'Check List'}
            },
            // Big Data
            {
                path: '/overview',
                component: load('views/BigData/Overview'),
                name: 'Overview',
                meta: {requiresAuth: true, desc: 'Market Overview'}
            },
            {
                path: '/occupancy',
                component: load('views/BigData/Occupancy'),
                name: 'Occupancy',
                meta: {requiresAuth: true, desc: 'Occupancy and available room'}
            },
            {
                path: '/ordertrend',
                component: load('views/BigData/OrderTrend'),
                name: 'OrderTrend',
                meta: {requiresAuth: true, desc: 'Order Trend'}
            },
            {
                path: '/profitforecast',
                component: load('views/BigData/ProfitForecast'),
                name: 'ProfitForecast',
                meta: {requiresAuth: true, desc: '收益测算'}
            },
            {
                path: '/orderanalysis',
                component: load('views/BigData/OrderAnalysis'),
                name: 'OrderAnalysis',
                meta: {requiresAuth: true, desc: 'Order Analysis'}
            },
            // Chat
            {
                path: '/chat/:mailbox',
                component: load('views/Chat'),
                name: 'Chat',
                meta: {requiresAuth: true, desc: 'Mail Chat List'}
            },
            // Bill
            {
                path: '/transactions',
                component: load('views/Bill/Transactions'),
                name: 'Transactions',
                meta: {requiresAuth: true, desc: 'Transactions List'}
            },
            {
                path: '/invoice',
                component: load('views/Bill/Invoice'),
                name: 'Invoice',
                meta: {requiresAuth: true, desc: 'Invoice List'}
            },
            {
                path: '/cleanerbill',
                component: load('views/Bill/CleanerBill'),
                name: 'CleanerBill',
                meta: {requiresAuth: true, desc: 'Clenaer Bill List'}
            },
            {
                path: '/checkaccounts',
                component: load('views/Bill/CheckAccounts'),
                name: 'CheckAccounts',
                meta: {requiresAuth: true, desc: 'Check Accounts List'}
            },
            {
                path: '/systemsettings',
                component: load('views/System/SystemSettings'),
                name: 'SystemSettings',
                meta: {requiresAuth: true, desc: 'System Settings'}
            },
        ]
    },
    {path: '/login', component: load('Login'), meta: {requiresAuth: false}},
    {path: '*', component: load('404'), meta: {requiresAuth: false}},
]

// Routing logic
let router = new Router({
    routes: routes,
    // mode: 'history',
    scrollBehavior: function (to, from, savedPosition) {
        return savedPosition || {x: 0, y: 0}
    }
})



const AUTHS_GOURP = ['admin', 'cs_thailand']

// Some middleware to help us ensure the user is authenticated.
router.beforeEach((to, from, next) => {
    if (to.meta.requiresAuth && (
        router.app.$store.state.token === null ||
        AUTHS_GOURP.indexOf(router.app.$store.state.token) === -1)) {
        next({
            path: '/login',
            query: {redirect: to.fullPath}
        })
    } else {
        // 默认进入order页面
        if (to.fullPath == '/') {
            next({
                path: "/order",
            })
        } else {
            next()
        }
    }
})

export default router
